﻿using MediatR;
using SmartTracking.API.Write.Application.Commands.Role_command;
using SmartTracking.Domain.UserInfo;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.Role_handler
{
    public class DeleteRoleCommandHandler:IRequestHandler<DeleteRoleCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<Roles> roleRep;
        private readonly IBaseRepository<RolePermissionsMiddle> rolePermissionRep;

        public DeleteRoleCommandHandler(
            IBaseRepository<Roles> roleRep,
            IBaseRepository<RolePermissionsMiddle> rolePermissionRep)
            {
            this.roleRep = roleRep;
            this.rolePermissionRep = rolePermissionRep;
        }

        public async Task<ApiResult<int>> Handle(DeleteRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.删除成功;
            result.Message = ApiEnum.删除成功.ToString();

            try
            {
                #region 删除角色
                Roles roles = roleRep.GetAll().FirstOrDefault(x => x.RoleId == request.RoleId);
                roles.IsDeleted = true;
                roles.DeleteTime = DateTime.Now;
                var list = await roleRep.UpdateAsync(roles);
                #endregion

                #region 删除角色权限
                #endregion

                result.data = list;
            }
            catch (Exception ex)
            {
                result.Code = ApiEnum.删除失败;
                result.Message = ApiEnum.删除失败.ToString() + "," + ex.Message;
                throw;
            }
            return await Task.FromResult(result);
        }
    }
}
